﻿#include"InventoryManagementView.h"
#include"XQTablePagingWidget.h"
#include"XQAlgorithm.h"
#include"InventoryView.h"
#include<QBoxLayout>
#include<QPushButton>
#include<QTableWidget>
InventoryManagementView::InventoryManagementView(QWidget* parent)
	:XQWidget(parent)
{
}

InventoryManagementView::~InventoryManagementView()
{
}

XQTablePagingWidget* InventoryManagementView::tableView() const
{
	return m_tableView;
}

InventoryModel InventoryManagementView::inventory(qint64 index)
{
	if (m_tableView->dataSize() <= index || index < 0)
		return InventoryModel();
	auto& item = *m_tableView->datas()[index];
	InventoryModel model;
	int nSel = 0;
	model.setInventory_id(item.data(nSel++).toULongLong());
	model.setName(item.text(nSel++));
	model.setType(item.text(nSel++));
	model.setCurrentInventory(item.data(nSel++).toULongLong());
	model.setSafetyInventory(item.data(nSel++).toULongLong());
	model.setRestockingQuantity(item.data(nSel++).toULongLong());
	model.setMTime(item.data(nSel++).toDateTime());
	model.setCTime(item.data(nSel++).toDateTime());
	return model;
}

void InventoryManagementView::setInventory(qint64 index, const InventoryModel& model)
{
	if (m_tableView->dataSize() <= index || index < 0)
		return;
	auto& item = *m_tableView->datas()[index];
	item.clear();
	item.push_back(QString::number(model.inventory_id()), model.inventory_id());
	item.push_back(model.name());
	item.push_back(model.type());
	item.push_back(QString::number(model.currentInventory()), model.currentInventory());
	item.push_back(QString::number(model.safetyInventory()), model.safetyInventory());
	item.push_back(QString::number(model.restockingQuantity()), model.restockingQuantity());
	item.push_back(model.mtime().toString("yyyy-MM-dd hh:mm:ss"), model.mtime());
	item.push_back(model.ctime().toString("yyyy-MM-dd hh:mm:ss"), model.ctime());
	m_type << model.type();
}

void InventoryManagementView::setInventory(const QList<InventoryModel>& list)
{
	clear();
	for (auto& data : list)
	{
		addInventory(data);
		m_type << data.type();
	}
}

void InventoryManagementView::addInventory(const InventoryModel& model)
{
	XQTablePagingItem item;
	item.push_back(QString::number(model.inventory_id()), model.inventory_id());
	item.push_back(model.name());
	item.push_back(model.type());
	item.push_back(QString::number(model.currentInventory()), model.currentInventory());
	item.push_back(QString::number(model.safetyInventory()), model.safetyInventory());
	item.push_back(QString::number(model.restockingQuantity()), model.restockingQuantity());
	item.push_back(model.mtime().toString("yyyy-MM-dd hh:mm:ss"), model.mtime());
	item.push_back(model.ctime().toString("yyyy-MM-dd hh:mm:ss"), model.ctime());
	m_tableView->push_backRow(item);
	m_type <<model.type() ;
}

void InventoryManagementView::removeInventory(const InventoryModel& inventory)
{
	m_tableView->removeShowData_if([=](const XQTablePagingItem& item) {
		if (item[0].data.toULongLong() == inventory.inventory_id())
			return true;
		});
	m_tableView->updataTable();
}

void InventoryManagementView::clear()
{
	m_tableView->clearData();
	m_tableView->clearTable();
	m_tableView->showTable();
}

void InventoryManagementView::showInventory(int page)
{
	m_tableView->showTable(page);
}

void InventoryManagementView::updataTable()
{
	m_tableView->updataTable();
}

QStringList InventoryManagementView::typeList()
{
	QStringList list;
	for (auto&data:m_type)
	{
		list << data;
	}
	return list;
}

void InventoryManagementView::init()
{
	XQWidget::init();
	init_table();
}

void InventoryManagementView::addOperate()
{
	auto view = new InventoryView;
	view->init();
	view->setAttribute(Qt::WA_DeleteOnClose);
	connect(this, &QObject::destroyed, view, &QObject::deleteLater);
	connect(view, &XQWidget::closeSignals, [=] {this->setEnabled(true); });
	connect(view, &InventoryView::okSignals, [=] {
		auto model = view->inventoryModel();
		model.setCTime();
		model.setMTime();
		model.setSafetyInventory(model.currentInventory()<model.safetyInventory()? model.safetyInventory()- model.currentInventory():0);
		emit addInventorySignals(model);
		});
	centerShow(view, this);
	this->setEnabled(false);
	view->setOkButtonName("添加");
	view->setWindowTitle("添加库存");
	view->setReadOnly(InventoryView::Type::IdType, true);
	view->setVisible(InventoryView::Type::RestockingQuantityType, false);
	view->setVisible(InventoryView::Type::MtimeType, false);
	view->setVisible(InventoryView::Type::CtimeType, false);
	setTranscribeWidget(CreateWidget::InventoryAddView,view);
	auto typeList = this->typeList();
	view->setTypeCompleter(typeList);
	view->setTypeItems(typeList);
	emit addInventoryCreateSignals(view);
}

void InventoryManagementView::modifyOperate()
{
	auto view = new InventoryView;
	view->init();
	view->setAttribute(Qt::WA_DeleteOnClose);
	connect(this, &QObject::destroyed, view, &QObject::deleteLater);
	connect(view, &XQWidget::closeSignals, [=] {this->setEnabled(true); });
	connect(view, &InventoryView::okSignals, [=] {
		emit modifyInventorySignals(view->inventoryModel());
		});
	centerShow(view, this);
	this->setEnabled(false);
	view->setOkButtonName("修改");
	view->setWindowTitle("编辑库存");
	view->setReadOnly(InventoryView::Type::IdType, true);
	view->setReadOnly(InventoryView::Type::MtimeType, true);
	view->setReadOnly(InventoryView::Type::CtimeType, true);
	view->setReadOnly(InventoryView::Type::RestockingQuantityType, true);
	setTranscribeWidget(CreateWidget::InventoryModifyView, view);
	view->setAutoRestockingQuantity(true);
	auto typeList = this->typeList();
	view->setTypeCompleter(typeList);
	view->setTypeItems(typeList);
	view->setInventoryModel(inventory(m_tableView->currentShowRow_toIndex()));
}

void InventoryManagementView::removeOperate()
{
	auto inventory = this->inventory(m_tableView->currentShowRow_toIndex());
	emit removeInventorySignals(inventory);
}

void InventoryManagementView::init_ui()
{
	XQWidget::init_ui();
	m_tableView = new XQTablePagingWidget(this);
	auto layout = new QBoxLayout(QBoxLayout::TopToBottom, widget());//从上到下
	layout->addWidget(init_operateButton());
	layout->addWidget(m_tableView);
	widget()->setLayout(layout);
}

void InventoryManagementView::init_table()
{
	//setContextMenuFunc();
	m_tableView->setHorizontalTitle({
		"编号","名称","类型","当前库存","安全库存","补货量","修改时间","创建时间"
		});
	m_tableView->tableWidget()->setSelectionBehavior(QAbstractItemView::SelectRows);
}

QWidget* InventoryManagementView::init_operateButton()
{

	m_addButton = new QPushButton("添加");
	m_modifyButton = new QPushButton("编辑");
	m_removeButton = new QPushButton("删除");
	connect(m_addButton, &QPushButton::pressed, this, &InventoryManagementView::addOperate);
	connect(m_modifyButton, &QPushButton::pressed, this, &InventoryManagementView::modifyOperate);
	connect(m_removeButton, &QPushButton::pressed, this, &InventoryManagementView::removeOperate);
	m_modifyButton->setEnabled(false);
	m_removeButton->setEnabled(false);
	connect(m_tableView->tableWidget(), &QTableWidget::itemSelectionChanged, [this] {
		if(!m_tableView->tableWidget()->selectedItems().isEmpty())
		{
			m_modifyButton->setEnabled(true);
			m_removeButton->setEnabled(true);
		}
		else
		{
			m_modifyButton->setEnabled(false);
			m_removeButton->setEnabled(false);
		}
		});
	auto layout = new QBoxLayout(QBoxLayout::LeftToRight);//从左到右
	layout->addWidget(m_addButton);
	layout->addWidget(m_modifyButton);
	layout->addWidget(m_removeButton);
	auto widget = new QWidget();
	setCssFile(widget, ":/XQWidget/style.css");
	widget->setLayout(layout);
	return widget;
}
